<template>
  <a-modal
    v-model:visible="visible"
    :title="title"
    :mask-closable="false"
    :esc-to-close="false"
    :modal-style="{ maxWidth: '520px' }"
    width="90%"
    @before-ok="save"
    @close="reset"
  >
    <a-form :model="form" :auto-label-width="true" layout="horizontal">
      <a-space direction="vertical" :style="{ width: '300px' }" style="margin: 0 auto">
        <a-form-item field="name" :style="{ width: '300px' }" label="双选会主题">
          <a-textarea v-model="form.title" placeholder="请输入双选会主题" max-length="64"/>
        </a-form-item>
        <a-form-item field="location" :style="{ width: '300px' }" label="举办地点">
          <a-input v-model="form.location" placeholder="请输入举办地点" />
        </a-form-item>
        <a-form-item field="name" :style="{ width: '300px' }" label="双选会介绍">
          <a-textarea v-model="form.description" placeholder="请输入双选会介绍" max-length="64"/>
        </a-form-item>
        <a-form-item field="type" :style="{ width: '300px' }" label="状态">
          <a-select v-model="form.status" placeholder="请选择状态">
            <a-option :value="0">未开始</a-option>
            <a-option :value="1">进行中</a-option>
            <a-option :value="2">已结束</a-option>
          </a-select>
        </a-form-item>
        <a-form-item field="startTime" :style="{ width: '300px' }" label="开始时间">
         <a-date-picker v-model="form.startDate">

         </a-date-picker>
        </a-form-item>
        <a-form-item field="endTime" :style="{ width: '300px' }" label="结束时间">
          <a-date-picker v-model="form.endDate">
          </a-date-picker>
        </a-form-item>
      </a-space>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
import { Message } from '@arco-design/web-vue'
import { getJobfair, addJobfair, updateJobfair } from '@/apis'
import { useForm } from '@/hooks'
import { useDict } from '@/hooks/app'

const emit = defineEmits<{
  (e: 'save-success'): void
}>()

const dataId = ref('')
const isUpdate = computed(() => !!dataId.value)
const title = computed(() => (isUpdate.value ? '修改双选会信息' : '新增双选会信息'))



const { form, resetForm } = useForm({
    // todo 待补充
  title: '',
  description: '',
  startDate: '',
  endDate: '',
  status: '',
  location: '',
})

// 重置
const reset = () => {
  resetForm()
}

const visible = ref(false)
// 新增
const onAdd = () => {
  reset()
  dataId.value = ''
  visible.value = true
}

// 修改
const onUpdate = async (id: string) => {
  reset()
  dataId.value = id
  const res = await getJobfair(id)
  Object.assign(form, res.data)
  visible.value = true
}

// 保存
const save = async () => {
  try {
    if (isUpdate.value) {
      await updateJobfair(form, dataId.value)
      Message.success('修改成功')
    } else {
      await addJobfair(form)
      Message.success('新增成功')
    }
    emit('save-success')
    return true
  } catch (error) {
    return false
  }
}

defineExpose({ onAdd, onUpdate })
</script>
